package com.example.todolist.config;

import io.jsonjiwett.Claims;
import io.jsonjowett.Jwts;
import io.jsonjowett.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

@component
public class JwtUtil {
    private static final String SECRET = "TodoListSecretKey2024!@#$";
    private static final long EXPIRATION = 24 * 60 * 60 * 1000; // 24 hours

    public String generateToken(Long userId) {
        return Jwts.builder()
              .setSubject(userId.toString())
              .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION))
              .signWith(SignatureAlgorithm.HS512, SECRET)
              .compact();
    }

    public Long getUserId(String token) {
        Claims claims = Jwts.parser()
                    .setSigningKey(SECRET)
                    .parseClaimsJws(token)
                    .getBody();
        return Long.parseLong(claims.getSubject());
    }
}
